/***************************** BEGIN LICENSE BLOCK ***************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/MPL-1.1.html
 
 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the License.
 
 The Original Code is the "SensorML DataProcessing Engine".
 
 The Initial Developer of the Original Code is the VAST team at the University of Alabama in Huntsville (UAH). <http://vast.uah.edu> Portions created by the Initial Developer are Copyright (C) 2007 the Initial Developer. All Rights Reserved. Please Contact Mike Botts <mike.botts@uah.edu> for more information.
 
 Contributor(s): 
    Alexandre Robin <robin@nsstc.uah.edu>
 
******************************* END LICENSE BLOCK ***************************/

package org.vast.sensorML;

import org.w3c.dom.*;
import java.net.*;
import org.vast.xml.*;


/**
 * <p>
 * Abstract SMLReader, base for all specific readers.
 * </p>
 *
 * <p>Copyright (c) 2005</p>
 * @author Alexandre Robin
 * @since Feb 16, 2006
 * @version 1.0
 */
public abstract class AbstractSMLReader
{
    
    
    /**
     * Resolve the IDREF attribute of the given element to a full URI 
     * @param elt Element to look into
     * @param refAttributeName Name of argument used to reference the ID (ex: href)
     * @return URI complete uri#id
     */
    public URI getResolvedIDRef(DOMHelper dom, Element elt, String refAttributeName) throws XMLReaderException
    {
        String uriText = dom.getAttributeValue(elt, refAttributeName);
        URI uri;
        
        try
        {
            uri = new URI(uriText);
        }
        catch (URISyntaxException e)
        {
            throw new XMLReaderException("Invalid URI (ID reference)", e);
        }
        
        URI docUri = dom.getParentDocument(elt).getUri();
        return docUri.resolve(uri);
    }



    /**
     * Resolve the ID attribute of the given element to a full URI
     * @param elt Element to look into
     * @return URI complete uri#id
     */
    public URI getResolvedID(DOMHelper dom, Element elt, String idAttributeName) throws XMLReaderException
    {
        String idText = dom.getAttributeValue(elt, idAttributeName);

        if (idText == null)
            return null;

        URI docUri = dom.getParentDocument(elt).getUri();
        return docUri.resolve("#" + idText);
    }
}
